package infoPackage;

import java.io.File;
import algorithms.GeneticAlgorithm;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;

public class Application {

    public static void main(String args[]) {
        String path = System.getProperty("user.dir") + "/files/rivers.xml";
        try {// Set System L&F
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            GenInterface genIterface = new GenInterface(path);
            //GenInterface genIterface = new GenInterface();
            genIterface.setVisible(true);
        } catch (UnsupportedLookAndFeelException e) {
            // handle exception
        } catch (ClassNotFoundException e) {
            // handle exception
        } catch (InstantiationException e) {
            // handle exception
        } catch (IllegalAccessException e) {
            // handle exception
        } catch (Exception e) {
            System.out.println("Unable to launch graphic mode. Entering text mode...\n");

            int nChromo = 10;
            LoadInfo loadinfo = new LoadInfo();
            try {
                loadinfo.xmlParser(initFile(args));
            } catch (ParserConfigurationException ex) {
                Logger.getLogger(Application.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SAXException ex) {
                Logger.getLogger(Application.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(Application.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            System.out.println("");

            GeneticAlgorithm genetics = new GeneticAlgorithm(loadinfo.nSections, loadinfo.rivers, loadinfo.sections, nChromo, true, 2, true,3,3);

            if ((genetics.elitismNum % 2) == 1 && genetics.elitism) {
                System.out.println("Invalid elistism number. Must be even. The application will exit.");
                System.exit(-3);
            }


            genetics.printPopulation(genetics.chromosomes);

            for (int i = 0; i < 4; i++) {
                System.out.println("\n########### Generation No " + i + " ###########\n");
                genetics.nextGeneration();
                System.out.println("\n############## ENDOF " + i + " ################\n");
            }
            System.out.println("************************* Final Popuplation ***********************\n");
            genetics.printPopulation(genetics.chromosomes);
        

        }
    }

    public static File initFile(String args[]) {
        String path = System.getProperty("user.dir") + "/files/rivers.xml";
        if (args.length == 0) {
            //System.out.println("Configuration file not specified. Program will exit.");
            //System.exit(-1);
            File file = new File(path);
            return file;
        }
        if (args.length == 1) {
            System.out.println(path);
            File file = new File(path);
            return file;
        }
        return null;
    }
}
